package com.doit.demo.day02.transformations;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.operators.StreamFilter;

/**
 * @DATE 2022/2/26/20:59
 * @Author MDK
 * @Version 2021.2.2
 *
 *  使用transform方法实现与filter类似的功能
 *
 **/
public class FilterDemo3 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> lines = env.socketTextStream("linux01", 8888);

        //输出以h开头的数据
        FilterFunction<String> filterFunction = new FilterFunction<String>() {
            @Override
            public boolean filter(String value) throws Exception {
                return value.startsWith("h");
            }
        };

        SingleOutputStreamOperator<String> filter = lines.transform("MyFilter", TypeInformation.of(String.class), new StreamFilter<>(filterFunction));

        filter.print();

        env.execute();
    }
}
